using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JmShared;
using System.Xml;
using System.Xml.Serialization;

namespace JiveMessengerLinux
{
    public static class StaticSettings
    {
        public const string ApplicationName = "JiveMessengerLinux";
        public const string VersionNumber = "1.0.0.3";
        //public const double Epsilon = 0.01;
        public static readonly string DataFolder = StaticMethods.CombinePath(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ApplicationName);

        private static StoredSettings _storedSettings;
        //public static string IconFolder
        //{
        //    get { return DataFolder + "Icons" + Path.DirectorySeparatorChar; }
        //}
        //static StaticSettings()
      //  {

            
    //
        public static void InitializeStaticSettings()
        {
        _storedSettings = Deserialize();
    }
        public static StoredSettings Deserialize()
        {
            StoredSettings settings = new StoredSettings();
            try
            {
            var serializer = new XmlSerializer(typeof(StoredSettings));
                if (!File.Exists(SettingsFile))//"JiveMessengerSettings.xml"))
                    return settings;
                using (var reader = XmlReader.Create(SettingsFile))//"JiveMessengerSettings.xml"))
            {
               settings = (StoredSettings)serializer.Deserialize(reader);
			
                if (settings != null)
                    return settings;
                    else
                        return new StoredSettings();

            }
            }catch(Exception)
            {
                return settings;
            }
        }

        public static void Serialize()
        {
            XmlSerializer serializer = null;
            XmlWriter writer = null;
            try
            {
                serializer = new XmlSerializer(typeof(StoredSettings));
                writer = XmlWriter.Create(SettingsFile);//"JiveMessengerSettings.xml");

		
                serializer.Serialize(writer, _storedSettings);
                writer.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }

        public static bool SettingStoreMessagesWhileOffline
        {
            get { return _storedSettings.SettingStoreMessagesWhileOffline; }
            set { _storedSettings.SettingStoreMessagesWhileOffline = value; }
        }
        public static int SettingOfflineMessagesAgeLimit
        {
            get { return _storedSettings.SettingOfflineMessagesAgeLimit; }
            set { _storedSettings.SettingOfflineMessagesAgeLimit = value; }
        }
        public static int SettingOfflineMessagesCountLimit
        {
            get { return _storedSettings.SettingOfflineMessagesCountLimit; }
            set { _storedSettings.SettingOfflineMessagesCountLimit = value; }
        }
        public static string Color
        {
            get { return _storedSettings.Color;}
            set { _storedSettings.Color = value;}
        }
        public static bool SettingStartWithWindows
        {
            get { return _storedSettings.StartWithWindows;}
            set { _storedSettings.StartWithWindows = value;}
        }
        public static Pango.FontDescription FontDescription
        {
            get{ return _storedSettings.FontDescription;}
            set { _storedSettings.FontDescription = value;}
        }

        public static bool SettingStartMinimized
        {
            get{ return _storedSettings.SettingStartMinimized;}
            set { _storedSettings.SettingStartMinimized = value;}
        }
        public static bool IsMaximized
        {
            get{ return _storedSettings.IsMaximized;}
            set { _storedSettings.IsMaximized = value;}
        }
//        public static bool IsIconized
//        {
//            get { return _storedSettings.IsIconized;}
//            set { _storedSettings.IsIconized = value;}
//        }
        public static int WindowX
        {
            get { return _storedSettings.WindowX;}
            set { _storedSettings.WindowX = value;}
        }
        public static int WindowY
        {
            get { return _storedSettings.WindowY;}
            set { _storedSettings.WindowY = value;}
        }
        public static int WindowSizeX
        {
            get{ return _storedSettings.WindowSizeX;}
            set { _storedSettings.WindowSizeX = value;}
        }
        public static int WindowSizeY
        {
            get { return _storedSettings.WindowSizeY;}
            set { _storedSettings.WindowSizeY = value;}
        }
        public static bool AutoLogin
        {
            get { return _storedSettings.AutoLogin;}
            set { _storedSettings.AutoLogin = value;}
        }
        public static bool RememberMe
        {
            get { return _storedSettings.RememberMe;}
            set { _storedSettings.RememberMe = value;}
        }
        public static bool RememberPassword
        {
            get { return _storedSettings.RememberPassword;}
            set  {_storedSettings.RememberPassword = value;}
        }
        public static bool SettingEnableNudge
        {
            get { return true;}
        }
        public static bool SettingEnableChatLog
        {
            get { return true;}
        }
        public static bool SettingEnableDebugLogging
        {
            get{ return _storedSettings.SettingEnableDebugLogging;}
            set { _storedSettings.SettingEnableDebugLogging = value;}
        }

        public static bool SettingGroupMessages
        {
            get{ return true;}
        }
        public static string SettingDebugLogLevel
        {
            get { return _storedSettings.SettingDebugLogLevel; }
            set { _storedSettings.SettingDebugLogLevel = value; }
        }
        public static bool SettingShowTimestamp
        {
            get { return true;}
        }
        public static string Server
        {
            get{ return _storedSettings.Server;}
            set{ _storedSettings.Server = value;}
        }

        public static string Username
        {
            get { return _storedSettings.Username;}
            set { _storedSettings.Username = value;}
        }

        public static string Password
        {
            get { return _storedSettings.Password;}
            set { _storedSettings.Password = value;}
        }

        public static string CustomEmoticonFolder
        {
            get { return StaticMethods.CombinePath(DataFolder, "CustomEmoticons"); }
        }

        public static string CustomContactEmoticonFolder
        {
            get { return StaticMethods.CombinePath(DataFolder, "Custom_Contact_Emoticons"); }
        }
        public static string SettingsFile
        {
            get { return StaticMethods.CombinePath(DataFolder, "JiveMessengerSettings.xml");}
        }
        public static string LogFolder
        {
            get { return StaticMethods.CombinePath(DataFolder, "Logs"); }
        }

        private static Dictionary<string[], string> _defaultEmoticons;

        public static Dictionary<string[], string> DefaultEmoticons
        {
            get { return _defaultEmoticons ?? (_defaultEmoticons = MakeDefaultEmoticonList()); }
        }

        public static  SerializableDictionary<string, SerializableDictionary<string, string>> CustomContactEmoticons
        {
            get{ return _storedSettings.CustomContactEmoticons;}
            set { _storedSettings.CustomContactEmoticons = value; }
        }

        public static SerializableDictionary<string, string> CustomEmoticons
        {
            get
            { 
                SerializableDictionary<string,string> foo = _storedSettings.CustomEmoticons;
                return foo;
            }
        }
        //		private static Dictionary<string, string> _customEmoticons;
        //		private static Dictionary<string, Dictionary<string, string>> _customContactEmoticons;
        //		public static Dictionary<string, Dictionary<string, string>> CustomContactEmoticons
        //		{
        //			get
        //			{
        //				if (_customContactEmoticons == null)
        //				{
        //					Dictionary<string, string> dic = StaticMethods.StringToDictionary<string, string>(Properties.Settings.Default.CustomContactEmoticons);
        //					Dictionary<string, Dictionary<string, string>> emotes = new Dictionary<string, Dictionary<string, string>>();
        //					foreach (KeyValuePair<string, string> kvp in dic)
        //					{
        //						Dictionary<string, string> emoticonFiles = StaticMethods.StringToDictionary<string, string>(kvp.Value).Where(kv => File.Exists(kv.Value)).ToDictionary(kv => kv.Key, kv => kv.Value);
        //						if (emoticonFiles.Count > 0 && !emotes.ContainsKey(kvp.Key))
        //							emotes.Add(kvp.Key, emoticonFiles);
        //					}
        //					_customContactEmoticons = emotes;
        //				}
        //				return _customContactEmoticons;
        //			}
        //set
        //{
        //    Dictionary<string, string> dic = new Dictionary<string, string>();
        //    foreach (KeyValuePair<string, Dictionary<string, string>> kvp in value)
        //    {
        //        string str = StaticMethods.DictionaryToString(kvp.Value);
        //        if (!dic.ContainsKey(str))
        //            dic.Add(str, str);
        //    }
        //    _customContactEmoticons = value;
        //    Properties.Settings.Default.CustomContactEmoticons = StaticMethods.DictionaryToString(dic);
        //}
        //		}
        //		public static Dictionary<string, string> CustomEmoticons
        //		{
        //			get { return _customEmoticons ?? (_customEmoticons = StaticMethods.StringToDictionary<string, string>(Properties.Settings.Default.CustomEmoticons)); }
        //			set
        //			{
        //				_customEmoticons = value;
        //				Properties.Settings.Default.CustomEmoticons = StaticMethods.DictionaryToString(_customEmoticons);
        //			}
        //		}
        public static SerializableDictionary<string, string> GetEmoticons(UserInfo user, bool includeDefault, bool includeCustom)
        {
            SerializableDictionary<string, string> allEmoticons = new SerializableDictionary<string, string> { { "<line>", "line.gif" }, { "<spacer>", "spacer.gif" } };
            if (includeCustom && !String.IsNullOrEmpty(user.UserID))
            {
                SerializableDictionary<string, string> ccEmoticons;
                CustomContactEmoticons.TryGetValue(user.UserID, out ccEmoticons);
				
                if (ccEmoticons != null && !user.Equals(Client.CurrentUserInfo))
                {
                    allEmoticons.AddRange(ccEmoticons);
                }
                else if (user.Equals(Client.CurrentUserInfo))
                {
                    allEmoticons.AddRange(CustomEmoticons);
                }
            }
            if (includeDefault)
            {
                //AllEmoticons.AddRange(DefaultEmoticons);
				
                foreach (KeyValuePair<string[], string> kvp in DefaultEmoticons)
                {
                    foreach (string s in kvp.Key)
                    {
                        string key = s.ToLower();
                        if (!allEmoticons.ContainsKey(key))
                            allEmoticons.Add(key, kvp.Value);
                        if (!allEmoticons.ContainsKey(s))
                            allEmoticons.Add(s, kvp.Value);
                    }
                }
            }
            return allEmoticons;
        }

        private static Dictionary<string[], string> MakeDefaultEmoticonList()
        {
            Dictionary<string[], string> defaultEmoticons = new Dictionary<string[], string>
            {
                {new[]{":)",":-)"}, "Smile.gif"},
                {new[]{":D",":-D"}, "Open_mouthed.gif"},
                {new[]{";)",";-)"}, "Wink_smile.gif"},
                {new[]{":O",":-O"}, "Surprised.gif"},
                {new[]{":P"}, "Tongue_out.gif"},
                {new[]{"(H)"}, "hot.gif"},
                {new[]{":@"}, "Angry.gif"},
                {new[]{":S",":-S"}, "Confused.gif"},
                {new[]{":$"}, "Embarrassed.gif"},
                {new[]{":(",":-("}, "Sad.gif"},
                {new[]{":'("}, "Crying.gif"},
                {new[]{":|"}, "Disappointed.gif"},
                {new[]{"(A)"}, "Angel.gif"},
                {new[]{"8o|"}, "Baring_teeth.gif"},
                {new[]{"8-|"}, "Nerd.gif"},
                {new[]{"+o("}, "Sick.gif"},
                {new[]{"|-)"}, "Sleepy.gif"},
                {new[]{"<:o)"}, "Party.gif"},
                {new[]{":^)"}, "I_dont_know.gif"},
                {new[]{":-#"}, "Not_Telling.gif"},
                {new[]{":-*"}, "Secret_telling.gif"},
                {new[]{"^o)"}, "Sarcastic.gif"},
                {new[]{"8-)"}, "Eye_rolling.gif"},
                {new[]{"(L)"}, "Red_heart.gif"},
                {new[]{"(U)"}, "Broken_heart.gif"},
                {new[]{"({)"}, "Left_hug.gif"},
                {new[]{"(})"}, "Right_hug.gif"},
                {new[]{"(bah)"}, "Sheep.gif"},
                {new[]{"*-)"}, "Thinking.gif"},
                {new[]{"(B)"}, "Beer.gif"},
                {new[]{"(@)"}, "Cat.gif"},
                {new[]{"(D)"}, "Cocktail.gif"},
                {new[]{"(C)"}, "Coffee.gif"},
                {new[]{"(6)"}, "Devil.gif"}
            };
			
            return defaultEmoticons;
        }
    }
}

